Practice (For)

  • 1 - 1000까지 정수 중 소수만 출력하시오.
  • 1 - 9000까지 정수 중 palindrome의 개수는? palindrome은 11, 121, 1331, 999 등 앞으로 읽으나 뒤로 읽으나 같은 수를 의미 합니다.
  • 15번째까지 피보나치 수열을 출력하시오. f0 = 1, f1 = 1, fn = fn-1 + fn-2
  • bubble sort implementation
  • selection sort implementation

In [24]:
import math

nums = range(3, 1001)
primes = [2]

for i in nums:
    is_prime = True
    for j in range(2, int(math.sqrt(i)+1)):
        if i % j == 0:
            is_prime = False
            break
    
    if is_prime:
        primes.append(i)
        
print primes, len(primes)


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] 168

In [10]:
nums = range(2, 1001)

for i, num in enumerate(nums):
    if num != 0:
        to_zero = i + num
        while to_zero < len(nums):
            nums[to_zero] = 0
            to_zero += num

primes = [p for p in nums if p!=0]
print primes, len(primes)


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997] 168

In [26]:
fibo = [1, 1]
for i in range(2, 15):
    fibo.append(fibo[i-1] + fibo[i-2])
    
print fibo[-1]


610

In [29]:
#bubble sort

arr = [9 , 1, 2, 15, 23, 7, 8, 6, 4, 3, 17]
count = len(arr)

for i in range(count):
    for j in range(1, count - i):
        if arr[j-1] > arr[j]:
            temp = arr[j]
            arr[j] = arr[j-1]
            arr[j-1] = temp
            
    print arr
print arr


[1, 2, 9, 15, 7, 8, 6, 4, 3, 17, 23]
[1, 2, 9, 7, 8, 6, 4, 3, 15, 17, 23]
[1, 2, 7, 8, 6, 4, 3, 9, 15, 17, 23]
[1, 2, 7, 6, 4, 3, 8, 9, 15, 17, 23]
[1, 2, 6, 4, 3, 7, 8, 9, 15, 17, 23]
[1, 2, 4, 3, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]

In [31]:
#selection sort

arr = [9, 1, 2, 15, 23, 7, 8, 6, 4, 3, 17]
count = len(arr)

for i in range(count):
    minimum = arr[i]
    minimum_idx = i
    for j in range(i + 1, count):
        if minimum > arr[j]:
            minimum = arr[j]
            minimum_idx = j
            
    temp = arr[i]
    arr[i] = arr[minimum_idx]
    arr[minimum_idx] = temp
    
    print arr
print arr


[1, 9, 2, 15, 23, 7, 8, 6, 4, 3, 17]
[1, 2, 9, 15, 23, 7, 8, 6, 4, 3, 17]
[1, 2, 3, 15, 23, 7, 8, 6, 4, 9, 17]
[1, 2, 3, 4, 23, 7, 8, 6, 15, 9, 17]
[1, 2, 3, 4, 6, 7, 8, 23, 15, 9, 17]
[1, 2, 3, 4, 6, 7, 8, 23, 15, 9, 17]
[1, 2, 3, 4, 6, 7, 8, 23, 15, 9, 17]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 23, 17]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 23, 17]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]
[1, 2, 3, 4, 6, 7, 8, 9, 15, 17, 23]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: